package com.filldream.fastboot.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.filldream.fastboot.base.entity.SysServerInfo;
import com.filldream.fastboot.base.mapper.SysServerInfoMapper;
import com.filldream.fastboot.base.request.pc.serverInfo.AddServerInfoSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.ModifyServerInfoSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.ServerInfoPageSTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.service.SysServerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysServerInfoServiceImpl extends ServiceImpl<SysServerInfoMapper, SysServerInfo> implements SysServerInfoService {

    @Override
    public B add(AddServerInfoSTO addServerInfoSTO) {
        checkHost(null,addServerInfoSTO.getServerHost());
        return B.checkSuccess( save(TransferUtil.changeToObj( addServerInfoSTO,SysServerInfo.class )) );
    }

    @Override
    public B modify(ModifyServerInfoSTO modifyServerInfoSTO) {
        checkHost(modifyServerInfoSTO.getId(),modifyServerInfoSTO.getServerHost());
        SysServerInfo serverInfo = TransferUtil.changeToObj(modifyServerInfoSTO, SysServerInfo.class);
        return B.checkSuccess( updateById( serverInfo ) );
    }

    @Override
    public B del(List<Long> ids) {
        ArrayList<SysServerInfo> list = new ArrayList<>();
        for (Long id : ids) {
            SysServerInfo item = new SysServerInfo();
            item.setId(id);
            item.setDelFlag(1);
            list.add( item );
        }
        return B.checkSuccess(updateBatchById(list));
    }

    @Override
    public P<SysServerInfo> queryPage(ServerInfoPageSTO sto) {

        LambdaQueryWrapper<SysServerInfo> sql = new LambdaQueryWrapper<>();
        sql.eq(SysServerInfo::getDelFlag,0);
        if( sto.getStatus() != null ){
            sql.eq(SysServerInfo::getStatus,sto.getStatus());
        }
        if(StringUtils.isNotBlank( sto.getKeyContent() )){
            sql.and(wq -> wq.like(SysServerInfo::getServerHost,sto.getKeyContent())
                    .or().like(SysServerInfo::getCloudUsername,sto.getKeyContent())
                    .or().like(SysServerInfo::getServerUsername,sto.getKeyContent()))
                    .or().like(SysServerInfo::getUsePurpose,sto.getKeyContent())
                    .or().like(SysServerInfo::getCloudProvider,sto.getKeyContent())
                    ;
        }

        Page<SysServerInfo> page = page(sto.page(), sql);
        List<SysServerInfo> records = page.getRecords();
        if(ObjectUtils.isNotEmpty(records)){
            for (SysServerInfo item : records) {
                item.setServerPassword("");
                item.setCloudPassword("");
                item.setCloudUsername("");
                item.setServerKey("");
                item.setServerSecret("");
                item.setServerUsername("");
            }
        }

        return P.autoSuccess(page);
    }

    public void checkHost(Long id,String host){
        SysServerInfo one = getOne(new LambdaQueryWrapper<SysServerInfo>().eq(SysServerInfo::getDelFlag, 0).eq(SysServerInfo::getServerHost,host));
        Assert.isBoolean( id == null && one != null , ErrorCode.BASE_ERR.reMsg("服务器 " + host + "已存在") );

        SysServerInfo oldServer = getById(id);
        Assert.isBoolean(
             one != null && one.getId().equals(id) && !oldServer.getServerHost().equals( host )
                ,ErrorCode.BASE_ERR.reMsg("服务器 " + host + "已存在")
        );

    }

}
